let App = getApp();
Page({
  data: {
    wxapp: [],
    fromtype:null,
    goods_list: [], // 商品列表
    order_total_num: 0,
    express_price:0,
    order_total_price: 0,
    order_pay_price:0,
    disabled: false,
    from_options:[],
    switch2Checked: false,
    switch2Style: 'text-decoration: line-through',
    score:0,
    integralOff:0,
    integral:0,
    use_score:0,
    score_pay:0
  },

  onLoad: function (options) {
    let that = this;
    console.log(options,'optionszzz')
    //页面启动后 调取首页的数据
    that.setData({
      wxapp: wx.getStorageSync('wxapp'),
      fromtype: options.type,
      from_options: options,
      ids:options.ids
    });
    App.wx_setcolor(that.data.wxapp);
  },

  onShow: function () {
    //这里根据类型  取出 数据
    let that = this;
    console.log(that.data,'allstart');
    if (that.data.fromtype =='cart'){
      App._post('cart/getlists', {ids:that.data.ids}, function (result) {
        console.log(result,'gouweuche');
        that.setData(result.data);
        that.setData({
          score:String(result.data.score),
          switch2Checked:false
        });
        //提示下架的商品
        if (result.data.error_msg != '') {
          App.showError(result.data.error_msg);
        }
      });
    }
    if (that.data.fromtype == 'buyNow') {
      App._get('order/buyNow', {
        goods_id: that.data.from_options.goods_id,
        goods_num: that.data.from_options.goods_num,
        goods_sku_id: that.data.from_options.goods_sku_id,
      }, function (result) {
        that.setData(result.data);
        that.setData({
          score:String(result.data.score),
          score_pay:result.data.score_pay,
        });
        if(that.data.from_options.scoreGood == '2' || that.data.from_options.scoreGood == '3'){
          that.setData({
            order_pay_price:result.data.score_pay,
          });
        }
        console.log(that.data,"sdfsdfsdnnnn52525")
        //提示下架的商品
        if (result.data.error_msg != null) {
          App.showError(result.data.error_msg);
        }
      });
    }
  },

  TapAdress: function () {
    wx.navigateTo({
      url: '../adress/index'
    });
  },
  onClicktjButton: function () {
    let that = this;

    if (that.data.goods_list.length==0) {
      App.showError('此订单无商品');
      return false;
    }

    if (that.data.disabled) {
      return false;
    }

    //这里开始提交订单
    console.log(that.data);
    if (that.data.has_error) {
      App.showError(that.data.error_msg);
      return false;
    }
    that.data.disabled = true;

    wx.showLoading({
      title: '正在处理...'
    });

    if (that.data.fromtype=='cart'){
      //提交订单
      App._post('order/cart_pay', {
        ids:that.data.ids,
        pay_score: that.data.use_score
      }, function (result) {
        //判断是否是积分直接购 弹窗并跳转
        if(result.code == 1){
          wx.showModal({
            title: '支付提示',
            content: result.msg,
            showCancel: false,
            success: function (res) {
              wx.reLaunch({
                url: '../order/index?showType=2'
              })
              // wx.navigateTo({
              //   url: '../order/index?showType=2'
              // })
            }
          });
          return true
        }
        //这里发起支付
        that.wx_pay_fun(result.data);
      }, function (result) {
        console.log(result);
      }, function () {
        that.data.disabled = false;
      });
    }
    if (that.data.fromtype == 'buyNow') {
      //提交订单
      App._post('order/buyNow_pay', {
        goods_id: that.data.from_options.goods_id,
        goods_num: that.data.from_options.goods_num,
        goods_sku_id: that.data.from_options.goods_sku_id,
        score: that.data.score_pay,
        type:that.data.from_options.scoreGood
        }, function (result) {
          if(result.code == 3){
            wx.showModal({
              title: '支付提示',
              content: result.msg,
              showCancel: false,
              success: function (res) {
                wx.reLaunch({
                  url: '../order/index?showType='+result.data
                })
                // wx.navigateTo({
                //   url: '../order/index?showType=2'
                // })
              }
            });
            return true
          }
        //这里发起支付
        that.wx_pay_fun(result.data);
      }, function (result) {
        console.log(result);
      }, function () {
        that.data.disabled = false;
      });
    }
  },

  wx_pay_fun: function (Rdata){
    // 发起微信支付
    wx.requestPayment({
      'timeStamp': Rdata.timestamp,
      'nonceStr': Rdata.nonceStr,
      'package': Rdata.package,
      'signType': Rdata.signType,
      'paySign': Rdata.paySign,
      success: function (res) {
        console.log('支付成功');
        // 跳转到订单展示界面
        wx.redirectTo({
          url: '../order/index?showType=2'
        })
      },
      fail: function (res) {
        console.log(res);
        App.showError('订单未支付', function () {
          // 跳转到未付款订单展示界面
          wx.redirectTo({
            url: '../order/index?showType=1'
          })
        });
      },
    });
  },
  switch2Change:function(e){
    console.log(e.detail.value,"勾选")
    var totalPrice = Number(this.data.order_total_price);
    var bl = Number(this.data.integral);
    var youfei = Number(this.data.express_price);
    var jf = Number(this.data.score)/bl;
    var totalPay;
    //如果积分超过产品价格 则抵消全部价格
    if(jf > totalPrice){
       jf = totalPrice;
    }
    this.setData({
      use_score: jf*bl
     })
    if(e.detail.value === true){
      //积分对冲订单价格
      totalPay = totalPrice + youfei - jf;
      this.setData({
        order_pay_price:totalPay
      })
     // this.data.order_pay_price = this.data.score/100 + this.data.express_price + this.data.order_total_price
      console.log(this.data.order_pay_price)
    }else{
      totalPay = totalPrice + youfei;
      this.setData({
        order_pay_price:totalPay,
        use_score: 0
      })
    }
  },
})